<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/25
 * Time: 16:00
 */

namespace app\index\controller;

use app\index\model\Grade as Grades;
use think\Request;

class Grade extends Base
{
    protected $Grade;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->Grade = new Grades();
    }

    public function index()
    {
        $list = $this->Grade->select();
        $this->assign('data', $list);
        return $this->fetch();
    }

    public function edit()
    {
        $id = input('get.id/d');
        if ($id) {
            $list = $this->Grade->where("id = $id")->find();
            $this->assign('data', $list);
        }
        return $this->fetch();
    }

    public function save()
    {
        $data = input('post.');
        $result = $this->validate($data, 'Grade');
        if (true !== $result) {
            $r = resposer($result, 0);
        } else {
            $data = $data['id'] ? $this->Grade->update($data) : $this->Grade->insert($data);
            if ($data) {
                $r = resposer('操作成功', 1);
            } else {
                $r = resposer('操作失败', 0);
            }
        }
        return $r;
    }

    public function delete()
    {
        $id = input('post.id/d');
        $r = resposer('删除失败', 0);
        if ($id) {
            if ($this->Grade->where(['id' => $id])->delete()) {
                $r = resposer(  '删除成功', 1);
            }
        }
        return $r;
    }
}